//
//  ParentRateView.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-23.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ParentRateView.h"
#import "RateView.h"
#import "NetworkAccessManager.h"
#import "AppDelegate.h"
#import "UIAlertView+Kirinrin.h"

@interface ParentRateView()
@property (weak, nonatomic) IBOutlet RateView *rate0;
@property (weak, nonatomic) IBOutlet RateView *rate1;
@property (weak, nonatomic) IBOutlet RateView *rate2;
@property (weak, nonatomic) IBOutlet RateView *rate3;
@property (weak, nonatomic) IBOutlet UITextView *message;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@end

@implementation ParentRateView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
//???: 用不到了
-(void)loadRemoteData{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    NSString *uriString = [NSString stringWithFormat:@"Score/%@.json" , @"f39c942b4569b70a014569c3a2400029"];
//    NSString *uriString = [NSString stringWithFormat:@"Score/%@.json" , appDelegate.user[@"childId"]];
    NSDictionary *parameters = @{@"key": appDelegate.accessKey};
    
    [[NetworkAccessManager shareManager]GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
            self.rate0.rating = [responseObject[@"data"][@"option0"]floatValue];
            self.rate1.rating = [responseObject[@"data"][@"option1"]floatValue];
            self.rate2.rating = [responseObject[@"data"][@"option2"]floatValue];
            self.rate3.rating = [responseObject[@"data"][@"option3"]floatValue];
            
            self.datetime = responseObject[@"data"][@"scoreDate"];
            [self.message setText:responseObject[@"data"][@"memo"]];
            
            [self.activityIndicator stopAnimating];
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [self.rate0 setMaxRating:3];
    [self.rate1 setMaxRating:3];
    [self.rate2 setMaxRating:3];
    [self.rate3 setMaxRating:3];
    self.rate0.editable = NO;
    self.rate1.editable = NO;
    self.rate2.editable = NO;
    self.rate3.editable = NO;
    if (!_data) {
        [self loadRemoteData];
    }else{
        [self.activityIndicator stopAnimating];
        
        self.rate0.rating = [_data[@"option0"]intValue];
        self.rate1.rating = [_data[@"option1"]intValue];
        self.rate2.rating = [_data[@"option2"]intValue];
        self.rate3.rating = [_data[@"option3"]intValue];
        
        self.datetime = _data[@"scoreDate"];
        [self.message setText:_data[@"memo"]];
        NSLog(@"---%@-->",_data[@"memo"]);
        if ([_data[@"memo"] isEqualToString:@""]) {
            [self.message setText:@"老师没有写评语呦!"];
        }
    }
}


@end
